let mazeSize = 20;
let bombCount = 2;
let playerX = 1;
let playerY = 1;
let endX;
let endY;
let prevMove = '';
let currentMazeArray;

const maze = document.getElementById('maze');
const bombCountElement = document.getElementById('bomb-count');

// 生成迷宫
function generateMaze() {
    const mazeArray = [];
    const wallProbability = 0.3;
    for (let i = 0; i < mazeSize; i++) {
        const row = [];
        for (let j = 0; j < mazeSize; j++) {
            if (i === 0 || i === mazeSize - 1 || j === 0 || j === mazeSize - 1) {
                row.push('#');
            } else {
                row.push(Math.random() < wallProbability ? '#' : ' ');
            }
        }
        mazeArray.push(row);
    }
    return mazeArray;
}

// 设置起点和终点
function setStartAndEnd(mazeArray) {
    for (let i = 1; i < mazeSize - 1; i++) {
        if (mazeArray[i][1] === ' ') {
            mazeArray[i][0] = 'S';
            break;
        }
    }
    for (let i = mazeSize - 2; i >= 0; i--) {
        if (mazeArray[i][mazeSize - 2] === ' ') {
            mazeArray[i][mazeSize - 1] = 'E';
            endX = mazeSize - 1;
            endY = i;
            break;
        }
    }
}

// 渲染迷宫
function renderMaze(mazeArray) {
    maze.style.gridTemplateColumns = `repeat(${mazeSize}, 20px)`;
    maze.style.gridTemplateRows = `repeat(${mazeSize}, 20px)`;
    maze.innerHTML = '';
    for (let i = 0; i < mazeSize; i++) {
        for (let j = 0; j < mazeSize; j++) {
            const cell = document.createElement('div');
            cell.classList.add('cell');
            if (mazeArray[i][j] === '#') {
                cell.classList.add('wall');
            } else if (mazeArray[i][j] === 'S') {
                cell.classList.add('start');
            } else if (mazeArray[i][j] === 'E') {
                cell.classList.add('end');
            } else if (i === playerY && j === playerX) {
                cell.classList.add('player');
            }
            maze.appendChild(cell);
        }
    }
}

// 玩家移动
function movePlayer(direction, mazeArray) {
    let newX = playerX;
    let newY = playerY;
    switch (direction) {
        case 'w':
            newY--;
            prevMove = 'w';
            break;
        case 's':
            newY++;
            prevMove = 's';
            break;
        case 'a':
            newX--;
            prevMove = 'a';
            break;
        case 'd':
            newX++;
            prevMove = 'd';
            break;
        case 'b':
            if (bombCount > 0) {
                let bombX = playerX;
                let bombY = playerY;
                switch (prevMove) {
                    case 'w':
                        if (bombY > 0 && mazeArray[bombY - 1][bombX] === '#') {
                            bombY--;
                        } else {
                            alert('无效的炮弹发射方向！');
                            return mazeArray;
                        }
                        break;
                    case 's':
                        if (bombY < mazeSize - 1 && mazeArray[bombY + 1][bombX] === '#') {
                            bombY++;
                        } else {
                            alert('无效的炮弹发射方向！');
                            return mazeArray;
                        }
                        break;
                    case 'a':
                        if (bombX > 0 && mazeArray[bombY][bombX - 1] === '#') {
                            bombX--;
                        } else {
                            alert('无效的炮弹发射方向！');
                            return mazeArray;
                        }
                        break;
                    case 'd':
                        if (bombX < mazeSize - 1 && mazeArray[bombY][bombX + 1] === '#') {
                            bombX++;
                        } else {
                            alert('无效的炮弹发射方向！');
                            return mazeArray;
                        }
                        break;
                    default:
                        alert('无效的炮弹发射方向！');
                        return mazeArray;
                }
                mazeArray[bombY][bombX] = ' ';
                bombCount--;
                bombCountElement.textContent = bombCount;
            } else {
                alert('没有炮弹剩余！');
            }
            return mazeArray;
        default:
            alert('无效的移动方向！');
            return mazeArray;
    }
    if (newX >= 0 && newX < mazeSize && newY >= 0 && newY < mazeSize && mazeArray[newY][newX]!== '#') {
        mazeArray[playerY][playerX] = ' ';
        playerX = newX;
        playerY = newY;
        mazeArray[playerY][playerX] = 'P';
    } else {
        alert('无效的移动方向！');
    }
    if (playerX === endX && playerY === endY) {
        if (confirm('恭喜，你赢了！是否要重新开始游戏？')) {
            resetGame();
        }
    }
    return mazeArray;
}

// 重置游戏
function resetGame() {
    playerX = 1;
    playerY = 1;
    prevMove = '';
    currentMazeArray = generateMaze();
    setStartAndEnd(currentMazeArray);
    currentMazeArray[playerY][playerX] = 'P';
    renderMaze(currentMazeArray);
}

// 初始化游戏
function initGame() {
    playerX = 1;
    playerY = 1;
    prevMove = '';
    currentMazeArray = generateMaze();
    setStartAndEnd(currentMazeArray);
    currentMazeArray[playerY][playerX] = 'P';
    renderMaze(currentMazeArray);
}

// 难度选择
function setDifficulty(size, bombNum) {
    mazeSize = size;
    bombCount = bombNum;
    bombCountElement.textContent = bombCount;
    resetGame();
}

document.getElementById('easy').addEventListener('click', function () {
    setDifficulty(15, 1);
});

document.getElementById('normal').addEventListener('click', function () {
    setDifficulty(25, 2);
});

document.getElementById('hard').addEventListener('click', function () {
    setDifficulty(30, 4);
});

document.getElementById('nightmare').addEventListener('click', function () {
    setDifficulty(35, 6);
});

document.addEventListener('keydown', function (event) {
    const key = event.key;
    if (key === 'q') {
        if (confirm('确定要退出吗？')) {
            window.close();
        }
    } else if (key === 'r') {
        resetGame();
    } else {
        currentMazeArray = movePlayer(key, currentMazeArray);
        renderMaze(currentMazeArray);
    }
});

initGame();